package com.javaweb.dao.impl;

import com.javaweb.dao.TagDao;
import com.javaweb.service.dto.TagDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class TagDaoImpl implements TagDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 获取所有标签
    @Override
    public List<TagDto> getAllTags() {
        String sql = "SELECT * FROM tb_tag";
        return jdbcTemplate.query(sql, (rs, rowNum) -> {
            TagDto tagDto = new TagDto();
            tagDto.setId(rs.getString("id"));
            tagDto.setName(rs.getString("name"));
            return tagDto;
        });
    }

    // 新增标签
    @Override
    public int addTag(TagDto tagDto) {
        String sql = "INSERT INTO tb_tag (name ) VALUES ( ?)";
        return jdbcTemplate.update(sql, tagDto.getName());
    }

    // 删除标签
    @Override
    public int deleteTagById(String Id) {
        String sql = "DELETE FROM tb_tag WHERE id = ?";
        return jdbcTemplate.update(sql, Id);
    }
}
